<?php
class ConfigModel extends Model{
	public $table='config';
	//修改配置
	public function updateConfig(){
		foreach ($_POST as $key => $value) {
			$data['value']=$value;
			if(!$this->where("name='$key'")->save($data)){
				$this->error='修改失败';
				return false;
			}
		}
		//更新配置文件
		return $this->createConfigFile();
	}
	//更新配置文件
	public function createConfigFile(){
		$config=$this->getField('name,value');

		$data = "<?php \nreturn ".var_export($config,true)."; \n?>";
		return file_put_contents(APP_CONFIG_PATH.'base.php', $data);

	}
	//设置模板风格
	public function setTplStyle(){
	  $dir =Q('dir');
	  $status = M('config')->where("name='WEBSTYLE'")->save(array('value'=>$dir));
	  return $this->createConfigFile();
	}
	//获得配置项
	public function get(){
		$config = $this->where("isshow=1")->order("orderlist desc ")->all();
		foreach ($config as $id => $c){
			$func = '_'.$c['type'];
			$config[$id]['_html']=$this->$func($c);
		}
		return $config;
	}
	public function _text($config){
		return "<input type='text' name='{$config['name']}' value='{$config['value']}' class='w200' />";
	}
	public function _textarea($config){
		return "<textarea name='{$config['name']}' class='w300 h80'>{$config['value']}</textarea>";
	}
	public function _radio($config){
		$radio = explode(',',$config['info']);
		$html='';
		foreach($radio as $r){
			$info = explode('|',$r);
			$checked=$info[0]==$config['value']?' checked="" ':'';
			$html.="<label><input type='radio' class='w30' $checked name='{$config['name']}' value='{$info[0]}'/> {$info[1]}</label>";
		}
		return $html;
	}
}

?>